package gui.buttons;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.geom.Matrix;
import events.ResizeEvent;
import flash.display.LineScaleMode;

/**
 * @author Damien Mabin
 */

class CheckBox extends Sprite
{

	// Taille du bord en pixel
	private var m_Style			: { backgroundColor : UInt, backgroundAlpha : Float, borderColor : UInt, borderAlpha : Float, borderSize : Int };
	
	// If true == checked, if false == nbot checked.
	private var m_State			: Bool;
	
	public function new(?f_Style : { backgroundColor : UInt, backgroundAlpha : Float, borderColor : UInt, borderAlpha : Float, borderSize : Int}) 
	{
		super();
		m_State = false;
		m_Style = ((f_Style == null) ? { backgroundColor : 0x000000, backgroundAlpha : 1.0, borderColor : 0x0000FF, borderAlpha : 1.0, borderSize : 0} : f_Style);
		
		// Initialisation de la couleur de fond
		this.graphics.clear();
		this.graphics.beginFill(m_Style.backgroundColor, m_Style.backgroundAlpha);
		if(m_Style.borderSize > 0)
			this.graphics.lineStyle(m_Style.borderSize, m_Style.borderColor, m_Style.borderAlpha);
		this.graphics.drawRect(0, 0, 10, 10);
		
		this.addEventListener(ResizeEvent.EVENT, OnResize);
		this.addEventListener(MouseEvent.CLICK, OnMouseEvent);
	}
	
	/**
	 * Use to state either if the check boc is checked or not.
	 * Behaviour : 
	 *     - If f_State == true, set the checkbox as checked.
	 *     - If f_State == false, set the checkbox as NOT checked.
	 * @param f_State The state you want to set.
	 */
	public function SetState(f_State:Bool):Void
	{
		// If the state don't change :
		if (f_State == m_State)
			return;
		else
		{
			m_State = f_State;
			OnResize(new ResizeEvent(width, height));
			// TODO : throw an event to warn about the fact that the state have changed !
		}
	}
	
	/**
	 * Return the current state of the checkbox.
	 * Behaviour : 
	 * 		- Return true is checked.
	 * 		- Return false if unchecked.
	 * @return the state of the checkbox.
	 */
	public function GetState() : Bool
	{
		return m_State;
	}
	
	private function OnMouseEvent(f_Evt : MouseEvent) : Void
	{
		switch(f_Evt.type)
		{
			case MouseEvent.CLICK : 
				SetState(!m_State);
		}
	}
	
	public function OnResize(f_Evt : ResizeEvent) : Void
	{
		this.graphics.clear();
		this.graphics.beginFill(m_Style.backgroundColor, m_Style.backgroundAlpha);
		if(m_Style.borderSize > 0)
			this.graphics.lineStyle(m_Style.borderSize, m_Style.borderColor, m_Style.borderAlpha);
		this.graphics.drawRect(0, 0, f_Evt.m_Width-1, f_Evt.m_Height-1);
		
		if (m_State)
		{
			this.graphics.moveTo(0, 0);
			this.graphics.beginFill(m_Style.borderColor);
			this.graphics.lineStyle(1, m_Style.borderColor, 1.0, false, LineScaleMode.NONE);
			this.graphics.lineTo(f_Evt.m_Width-1, f_Evt.m_Height-1);
			this.graphics.moveTo(0, f_Evt.m_Height-1);
			this.graphics.lineTo(f_Evt.m_Width-1, 0);
			this.graphics.endFill();
		}
    }
}